import { Controller } from 'egg';
import { Result } from '../util';
import { uuidCommonRule } from './validate-rule';

class UserController extends Controller {
  async answerProgressState() {
    const { ctx } = this;

    ctx.validate({
      tel: {
        type: 'string',
        max: 11,
        min: 11
      },
      uuid: uuidCommonRule
    }, ctx.query);

    const { tel, uuid } = ctx.query;

    const answerProgressState = await ctx
      .service
      .user
      .answerProgressState(
        tel, uuid
      );

    ctx.body = new Result({
      ...answerProgressState,
      verifyMessageState: 1
    });
  }

  async userInfo() {
    const { ctx } = this;

    ctx.validate({
      questionnaireUuid: uuidCommonRule,
      userUuid: uuidCommonRule,
      email: {
        type: 'string',
        required: false
      },
      gender: {
        type: 'enum',
        values: [1, 2],
        convertType: 'number'
      },
      maritalStatus: {
        type: 'enum',
        values: [0, 1],
        convertType: 'number'
      },
      childrenSituation: {
        type: 'enum',
        values: [0, 1],
        convertType: 'number'
      },
      age: {
        type: 'enum',
        values: [1, 2, 3, 4],
        convertType: 'number'
      },
      education: {
        type: 'enum',
        values: [1, 2, 3, 4],
        convertType: 'number'
      },
      startTime: {
        type: 'number'
      }
    }, ctx.request.body);

    const {
      questionnaireUuid,
      name,
      num,
      jobPosition,
      affiliated,
      workUnit,
      operatingPost
    } = ctx.request.body;

    const {
      userNameVisible,
      userNumVisible,
      jobPositionVisible,
      affiliatedVisible,
      workUnitVisible,
      operatingPostVisible
    } = await ctx
      .service
      .questionnaire
      .findQuestionnaireByUuid(questionnaireUuid);

    const rule: {
      name?: { type: string },
      num?: { type: string },
      jobPosition?: { type: string },
      affiliated?: { type: string },
      workUnit?: { type: string },
      operatingPost?: { type: string }
    } = {};

    if (userNameVisible) {
      rule.name = { type: 'string' };
    }

    if (userNumVisible) {
      rule.num = { type: 'string' };
    }

    if (jobPositionVisible) {
      rule.jobPosition = { type: 'string' };
    }

    if (affiliatedVisible) {
      rule.affiliated = { type: 'string' };
    }

    if (workUnitVisible) {
      rule.workUnit = { type: 'string' };
    }

    if (operatingPostVisible) {
      rule.operatingPost = { type: 'string' };
    }

    ctx.validate(rule, ctx.request.body);

    if (
      !userNameVisible
      && name
    ) {
      ctx.status = 422;
      ctx.body = new Result(undefined, '不应存在姓名！');
      return;
    }

    if (
      !userNumVisible
      && num
    ) {
      ctx.status = 422;
      ctx.body = new Result(undefined, '不应存在工号！');
      return;
    }

    if (
      !jobPositionVisible
      && jobPosition
    ) {
      ctx.status = 422;
      ctx.body = new Result(undefined, '不应存在工作职位！');
      return;
    }

    if (
      !affiliatedVisible
      && affiliated
    ) {
      ctx.status = 422;
      ctx.body = new Result(undefined, '不应存在附属单位！');
      return;
    }

    if (
      !workUnitVisible
      && workUnit
    ) {
      ctx.status = 422;
      ctx.body = new Result(undefined, '不应存在工作单位！');
      return;
    }

    if (
      !operatingPostVisible
      && operatingPost
    ) {
      ctx.status = 422;
      ctx.body = new Result(undefined, '不应存在工作岗位！');
      return;
    }

    const userInfoParams = ctx.request.body;

    const updateResult = await ctx.service.user.userInfo(userInfoParams);

    if (!updateResult) {
      ctx.status = 500;
      ctx.body = new Result(undefined, '用户信息存储失败！');
      return;
    }

    ctx.body = new Result({ mergeUuid: updateResult });
  }

  // 获取用户基本信息
  async basicInfo() {
    const { ctx } = this;

    ctx.validate({
      mergeUuid: uuidCommonRule,
    }, ctx.query);

    const { mergeUuid } = ctx.query;
    const basicInfo = await ctx.service.user.findUserByMergeUuid(mergeUuid);
    ctx.body = new Result(basicInfo);
  }

  async existTel() {
    const { ctx } = this;

    ctx.validate({
      tel: {
        type: 'string',
        max: 11,
        min: 11
      },
    }, ctx.query);

    const { tel } = ctx.query;

    const result = await ctx.service.user.existTel(tel);

    ctx.body = new Result(result);
  }
}

export default UserController;
